﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace LOLServer.Tools
{
    /// <summary>
    /// 单线程委托
    /// </summary>
    public delegate void ExecutorDelegate();
    /// <summary>
    /// 单线程处理对象
    /// </summary>
    public class ExecutorPool
    {
        private static ExecutorPool instance;
        Mutex tex = new Mutex();
        public static ExecutorPool Instance
        {
            get
            {
                if (instance == null)
                {
                    return new ExecutorPool();
                }
                return instance;
            }
        }
        /// <summary>
        /// 单线程处理逻辑
        /// </summary>
        /// <param name="ed"></param>
        public void Executor(ExecutorDelegate ed)
        {
            lock (this)
            {
                tex.WaitOne();//如果正在处理当前事务，则加锁挂起
                ed();//执行传进来的委托函数
                tex.ReleaseMutex();//释放锁，让下一个事务继续处理
            }
        }
    }
}
